package com.xianshiyue.listener;

import com.xianshiyue.Customer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Project  : mi
 * Author   : Wu Tian Qiang
 * Date     : 2016/6/22
 */
public class SystemListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        final Customer c = new Customer();
        new Thread(new Runnable() {
            java.util.Random rand = new java.util.Random();

            @Override
            public void run() {

                while (c.loop) {
                    c.findMi5();
                    sleepNext(500);
                    c.findHand2();

                    sleepNext(3000);
                }
                c.send("服务器程序结束运行！");
            }


            private void sleepNext(int ms) {
                // 随机暂停500耗秒
                try {
                    Thread.sleep(rand.nextInt(ms));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        System.out.println("系统初始化完成...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

}
